<?php
/**
* @author Amasty Team
* @copyright Amasty
* @package Amasty_Flags
*/
class Amasty_Flags_Block_Rewrite_Adminhtml_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
    protected function _prepareCollection()
    {
        if (method_exists($this, '_getCollectionClass'))
        {
            // for 1.4.1.+
            $collection = Mage::getResourceModel($this->_getCollectionClass());
            $mainAlias  = 'main_table';
        } else 
        {
            // for 1.4.0.x
            $collection = Mage::getResourceModel('sales/order_collection')
                ->addAttributeToSelect('*')
                ->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left')
                ->joinAttribute('billing_lastname', 'order_address/lastname', 'billing_address_id', null, 'left')
                ->joinAttribute('shipping_firstname', 'order_address/firstname', 'shipping_address_id', null, 'left')
                ->joinAttribute('shipping_lastname', 'order_address/lastname', 'shipping_address_id', null, 'left')
                ->addExpressionAttributeToSelect('billing_name',
                    'CONCAT({{billing_firstname}}, " ", {{billing_lastname}})',
                    array('billing_firstname', 'billing_lastname'))
                ->addExpressionAttributeToSelect('shipping_name',
                    'CONCAT({{shipping_firstname}},  IFNULL(CONCAT(\' \', {{shipping_lastname}}), \'\'))',
                    array('shipping_firstname', 'shipping_lastname'));
            $mainAlias  = 'e';
        }
        // joining order flag priority to the collection
        $collection->getSelect(
        )->joinLeft(
            array('f2o' => Mage::getModel('amflags/order_flag')->getResource()->getMainTable()),
            'f2o.order_id = ' . $mainAlias . '.entity_id',
            array()
        )->joinLeft(
            array('f' => Mage::getModel('amflags/flag')->getResource()->getMainTable()),
            'f.entity_id = f2o.flag_id',
            array('flag_priority' => 'priority')
        );
        $this->setCollection($collection);
        return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
    }
    
    protected function _prepareColumns()
    {
        parent::_prepareColumns();

        $flagColumn = $this->getLayout()->createBlock('adminhtml/widget_grid_column')
                      ->setData(array(
                          'header'      => Mage::helper('amflags')->__('Flag'),
                          'index'       => 'flag_priority',
                          'width'       => '40px',
                          'align'       => 'center',
                          'filter'      => false,
                          'renderer'    => 'amflags/adminhtml_renderer_flag',
                      ))
                      ->setGrid($this)
                      ->setId('flag_id');
        // adding flag column to the beginning of the columns array
        $flagColumnArray = array('flag_id' => $flagColumn);
        $this->_columns = $flagColumnArray + $this->_columns;
        
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('DESC');
        $this->sortColumnsByOrder();
        
        return $this;
    }
}
